package com.kms.servlet;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kms.derby.Connections;
import com.kms.model.SystemParameterBean;
  
public class DownServlet extends HttpServlet {  
  
    private static final long serialVersionUID = 8594448765428224944L;  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
  
        doPost(request, response);  
    }  
  
    /* 
     * 处理请求 (non-Javadoc) 
     *  
     * @see 
     * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest 
     * , javax.servlet.http.HttpServletResponse) 
     */  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
    	Map<String,SystemParameterBean> mapv = getMapSysSetting(request);
        //接收中文参数乱码处理  
        response.setCharacterEncoding("GBK");  
        String name=null;
		try {
			name = new String(request.getParameter("fileName").getBytes("ISO-8859-1"), "GBK");
		} catch (Exception e1) {
		}  
        
        if(name!=null){
            //得到文件名  
            // web绝对路径  
            String path = request.getSession().getServletContext().getRealPath("/");  
            String savePath = path + "upload";  
            if(name.contains("log")){
            	savePath = mapv.get("LogDirectory").getValue();
            }else if(name.contains("kms") || name.contains("bond")){
            	savePath = mapv.get("LinuxDirectory").getValue();
            }
      
            // 设置为下载application/x-download  
            response.setContentType("application/x-download");  
            // 即将下载的文件在服务器上的绝对路径  
            String filenamedownload = savePath + "/" + name;  
            // 下载文件时显示的文件保存名称  
            String filenamedisplay = name;  
            // 中文编码转换  
            filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");  
            response.addHeader("Content-Disposition", "attachment;filename="+ filenamedisplay);  
            try {  
                java.io.OutputStream os = response.getOutputStream();  
                java.io.FileInputStream fis = new java.io.FileInputStream( filenamedownload);  
                byte[] b = new byte[2048];  
                int i = 0;  
                while ((i = fis.read(b)) > 0) {  
                    os.write(b, 0, i);  
                }  
                fis.close();  
                os.flush();  
                os.close();  
            } catch (Exception e) {  
      
            }  
        }else{
        	String dir = mapv.get("LinuxDirectory").getValue();
    		request.setAttribute("dir", dir);
    		request.getRequestDispatcher("downfileview.jsp").forward(request,response); 
        }
 
    }  
    
	public Map<String,SystemParameterBean> getMapSysSetting(HttpServletRequest request){
		Map<String,SystemParameterBean> mapv = new HashMap<String,SystemParameterBean>();
		List<SystemParameterBean> listsysbean = new ArrayList<SystemParameterBean>();
		try {
			Connection conn = Connections.getConnection(request);
			String SQL = "SELECT * FROM SYSSETTING";
			PreparedStatement stmt = conn.prepareStatement(SQL);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				SystemParameterBean sysb = new SystemParameterBean();
				sysb.setId(rs.getInt("Id"));
				sysb.setName(rs.getString("Name"));
				sysb.setType(rs.getString("Type"));
				sysb.setValue(rs.getString("Value"));
				sysb.setEnabled(rs.getString("Enabled"));
				sysb.setDescription(rs.getString("Description"));
				mapv.put(rs.getString("Name"), sysb);
			}
			
			rs.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mapv;
	}
}  